<?php
/**
 * Description of Product
 *
 * @author Lukas Normantas
 */
class Catalog_Model_Product extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('catalog_products');
        $this->hasColumn('category_id', 'integer');
        $this->hasColumn('title', 'string', 50);
        $this->hasColumn('short_description', 'string', 255);
        $this->hasColumn('description', 'clob');
        $this->hasColumn('price', 'float');
        $this->hasColumn('weight', 'float');
        $this->hasColumn('active', 'boolean', 25);
    }

    public function setUp()
    {
        $this->actAs('Timestampable');

        $this->hasOne('Catalog_Model_Category as Category', array(
            'local' => 'category_id',
            'foreign' => 'id'
        ));

        $this->hasMany('Catalog_Model_Special as Specials', array(
            'local' => 'id',
            'foreign' => 'product_id'
        ));
    }

    /**
     * @var Zend_Form
     */
    protected $_form = null;

    public function getForm($product_id = null){
        if ($this->_form == null){
            $this->_form = new Catalog_Form_ProductEditing();
            $this->_form->setDefaults($this->toArray());
        }
        return $this->_form;
    }

    /**
     * @var Zend_Form
     */
    protected $_moveForm = null;

    public function getMoveForm($product_id = null){
        if ($this->_moveForm == null){
            $this->_moveForm = new Catalog_Form_CategorySelect();
            //$this->_moveForm->setDefaults($this->toArray());
            $this->_moveForm->setDefault('selected_category_id', $this->category_id);

        }
        return $this->_moveForm;
    }

    public function edit($data)
    {
        $form = $this->getForm();
        if ($form->isValid( $data )){
            $this->fromArray($form->getValues());
            $this->save();            
            $upload = $form->foo->getTransferAdapter();
            $upload->receive();
            $filename =$upload->getFileName();
            if ($upload->isReceived() && ! empty($filename)){
                $file = DATA_PATH . 'catalog_images/product-'.$this->id.'.jpg';
                unlink($file);
                $filterFileRename = new Zend_Filter_File_Rename($file);
                $filterFileRename->filter($filename);
            }
            return true;
        } 
        return false;
    }    

    public function move($data)
    {
        $form = $this->getMoveForm();
        if ($form->isValid( $data )){
            $this->fromArray($form->getValues());
            $this->category_id = $form->getValue('selected_category_id');
            $this->save();
            return true;
        }
        return false;
    }

    static public function getThumb($product_id)
    {
        $content = Areal_Image::getThumbContent(DATA_PATH
                                                . 'catalog_images/product-'.$product_id.'.jpg');
        return $content;
    }


    static public function getImage($product_id)
    {
        $content = Areal_Image::getImageContent(DATA_PATH
                                                . 'catalog_images/product-'.$product_id.'.jpg');
        return $content;
    }


}